package cn.thoughtworks.school.contract;

import cn.thoughtworks.school.Application;
import cn.thoughtworks.school.services.JenkinsService;
import cn.thoughtworks.school.services.UserCenterService;
import com.github.database.rider.core.api.configuration.DBUnit;
import com.github.database.rider.core.api.dataset.DataSet;
import com.github.database.rider.spring.api.DBRider;
import io.restassured.module.mockmvc.RestAssuredMockMvc;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.cloud.contract.stubrunner.spring.AutoConfigureStubRunner;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.WebApplicationContext;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@Ignore
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@AutoConfigureStubRunner
@DirtiesContext
@ActiveProfiles("test")
@Transactional
@DBRider
@DBUnit(caseSensitiveTableNames = true)
@DataSet("stack.yml")
public class StackBase {

    @Autowired
    private WebApplicationContext wac;

    @MockBean
    private JenkinsService jenkinsService;
    @MockBean
    private RestTemplate restTemplate;

    @Before
    public void setup() {
        Mockito.when(jenkinsService.addStack(Mockito.anyString(), Mockito.anyString())).thenReturn("SUCCESS");

        List<Map> list = new ArrayList<>();
        Map<String, String> restTemplateShouldReturn = new LinkedHashMap<>();
        restTemplateShouldReturn.put("layer", "");
        restTemplateShouldReturn.put("name", "alpine");
        list.add(restTemplateShouldReturn);

        ResponseEntity responseEntity = new ResponseEntity<>(list, HttpStatus.OK);
        when(restTemplate.getForEntity(anyString(), any(Class.class))).thenReturn(responseEntity);
        when(restTemplate.postForObject(anyString(), any(Object.class), any(Class.class))).thenReturn(null);

        RestAssuredMockMvc.webAppContextSetup(wac);
    }
}
